package com.zjg.servlet;

import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.lang.reflect.Array;
import java.util.HashMap;
import java.util.Map;
import java.util.Objects;

import javax.servlet.ServletException;
import javax.servlet.annotation.MultipartConfig;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.Part;

import com.alibaba.fastjson.JSONObject;
import com.zjg.bean.User;
import com.zjg.service.RegisterServiceImp;
import com.zjg.service.UserServiceImp;
import com.zjg.service.iface.RegisterService;
import com.zjg.service.iface.UserService;

/**
 * Servlet implementation class RegisterServlet
 */
@WebServlet("/register.servlet")
@MultipartConfig
public class RegisterServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
	
	protected void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		
		RegisterService rs = new RegisterServiceImp();
		UserService us=new UserServiceImp();
		request.setCharacterEncoding("utf-8");
		response.setCharacterEncoding("utf-8");
		response.setContentType("text/html; charset=utf-8");
		String param = request.getParameter("param");
		String userName = request.getParameter("userName");
		String userid = request.getParameter("userID");
		String password=request.getParameter("password");
		String sex=request.getParameter("sex");
		String []hobbys=request.getParameterValues("fav");
		String code=request.getParameter("city");
		String email = request.getParameter("email");
		String nikname=request.getParameter("nikname");
		if(Objects.equals("register", param)) {
			
			Part part = request.getPart("headicon");
			
			 InputStream input=part.getInputStream();
			 
			 
			int id = Integer.parseInt(userid);
			int sexi = Integer.parseInt(sex);
			int[] hobby = new int[hobbys.length];
			for (int i = 0; i < hobbys.length; i++) {
				hobby[i] = Integer.parseInt(hobbys[i]);
				
			}

			User user = new User(id, userName, password, sexi, hobby, code, email);
			user.setNikname(nikname);
			if (rs.Register(user,input)) {
				request.getRequestDispatcher("login.jsp").forward(request, response);

			} else {

			}

		}
		if(Objects.equals("useredit",param)) {

			
			int id = Integer.parseInt(userid);
			int sexi = Integer.parseInt(sex);
			int[] hobby = new int[hobbys.length];
			for (int i = 0; i < hobbys.length; i++) {
				hobby[i] = Integer.parseInt(hobbys[i]);
				
			}

			User user = new User(id, userName, password, sexi, hobby, code, email);
			user.setNikname(nikname);
			if (us.EditUser(user)) {
				request.getRequestDispatcher("views/Useredit.jsp").forward(request, response);

			} else {

			}

		
		}

		if (Objects.equals("testusername", param)) {

			
			Map<String,String> map = new HashMap<String,String>();
			map.put("mes","getDate");
			if (userName != null && userName != "") {
				if (rs.CheckUserName(userName)) {
					map.put("data", "have");
				} else {
					map.put("data", "pass");
				}
				;
			} else {
				map.put("data", "notnull");
			}
			String json = JSONObject.toJSONString(map);
			PrintWriter out = response.getWriter();
			out.println(json);
			out.flush();
			out.close();
		}
		if (Objects.equals("testuserid", param)) {

			
			Map<String,String> map = new HashMap<String,String>();
			map.put("mes", "getDate");
			if (userid != null && userid != "") {
				if (rs.CheckUserId(userid)) {
					map.put("data", "have");
				} else {
					map.put("data", "pass");
				}
				;
			} else {
				map.put("data", "notnull");
			}
			String json = JSONObject.toJSONString(map);
			PrintWriter out = response.getWriter();
			out.println(json);
			out.flush();
			out.close();
		}
		

	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		doGet(request, response);
	}

}
